﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace WeFrame.AutoCode
{
    class EntityHelper
    {
        public string GeneraterEntityText(SqlConnection connection, string databaseName, string tableName,string nameSpace)
        { 
           //产生实体的代码
            
            return ""; 
        }

        /// <summary>
        /// 根据表名和列集合生产表的实体文本
        /// </summary>
        /// <param Name="TableName"></param>
        /// <param Name="columnList"></param>
        /// <returns></returns>
        public string GenerateEntityText(TableEntity tableEntity, List<ColumnEntity> columnList, ConfigParameter configParameter)
        {

            StringBuilder sb = new StringBuilder();
            string blankspace4 = UtilsHelper.CreateBlankSpace(4);
            string blankspace8 = UtilsHelper.CreateBlankSpace(8);
            string blankspace12 = UtilsHelper.CreateBlankSpace(12);
            sb.Append("using System;" + Environment.NewLine);
            sb.Append("using System.Collections.Generic;" + Environment.NewLine);
            sb.Append("using System.Text;" + Environment.NewLine);
            sb.Append("namespace "+configParameter.NameSpaceTag+".Model"+Environment.NewLine);
            sb.Append("{" + Environment.NewLine);
            sb.Append("    [Serializable]" + Environment.NewLine);
            sb.Append("    public class " + tableEntity.TableName+configParameter.EntityTag + Environment.NewLine);
            sb.Append(blankspace4 + "{" + Environment.NewLine);
            foreach (ColumnEntity columnEntity in columnList) 
            {
                string name = columnEntity.Name;
                string lowerName = UtilsHelper.LowerFirstChar(name);
                string type = UtilsHelper.DbTypeToCodeType(columnEntity.Type);
                sb.Append(blankspace8 + "private " + type + " " + lowerName + ";" + Environment.NewLine);
                sb.Append(blankspace8 + "/// <summary>" + Environment.NewLine);
                sb.Append(blankspace8 + "/// 属性：" + columnEntity.Description + Environment.NewLine);
                sb.Append(blankspace8 + "/// 默认值：" + columnEntity.DefaultValue + Environment.NewLine);
                sb.Append(blankspace8 + "/// </summary>" + Environment.NewLine);
                sb.Append(blankspace8 + "public " + type + " " + name + Environment.NewLine);
                sb.Append(blankspace8 + "{" + Environment.NewLine);
                sb.Append(blankspace12 + "get { return " + lowerName + ";}" + Environment.NewLine);
                sb.Append(blankspace12 + "set { " + lowerName + "=value; }" + Environment.NewLine);
                sb.Append(blankspace8 + "}" + Environment.NewLine);
            }
            sb.Append(blankspace4 + "}" + Environment.NewLine);
            sb.Append("}" + Environment.NewLine);
            return sb.ToString();
        }
    }
}
